package com.zbkj.crmeb.chant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.chant.dao.ChannelDiscountPackageGoodsDao;
import com.zbkj.crmeb.chant.model.ChannelDiscountPackage;
import com.zbkj.crmeb.chant.model.ChannelDiscountPackageGoods;
import com.zbkj.crmeb.chant.response.ChannelDiscountPackageResponse;
import com.zbkj.crmeb.chant.service.ChannelDiscountPackageGoodsService;
import com.zbkj.crmeb.chant.service.ChannelDiscountPackageService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChannelDiscountPackageGoodsServiceImpl extends ServiceImpl<ChannelDiscountPackageGoodsDao, ChannelDiscountPackageGoods> implements ChannelDiscountPackageGoodsService {

    @Autowired
    private ChannelDiscountPackageService channelDiscountPackageService;

    @Override
    public List<ChannelDiscountPackageGoods> getList(Integer activityId) {
        return this.list(Wrappers.<ChannelDiscountPackageGoods>lambdaQuery().eq(ChannelDiscountPackageGoods::getActivityId, activityId));
    }

    @Override
    public List<ChannelDiscountPackageResponse> getProductDiscountInfo(Integer productId, Integer merId) {
        List<ChannelDiscountPackageResponse> responses = new ArrayList<>();
        // 商品所属的全部套餐
        List<ChannelDiscountPackageGoods> channelDiscountPackageGoods = this.list(Wrappers.<ChannelDiscountPackageGoods>lambdaQuery().eq(ChannelDiscountPackageGoods::getProductId, productId));
        if (CollectionUtils.isEmpty(channelDiscountPackageGoods)) {
            return responses;
        }
        // 所有套餐id
        List<Integer> activityIds = channelDiscountPackageGoods.stream().map(ChannelDiscountPackageGoods::getActivityId).distinct().collect(Collectors.toList());
        List<ChannelDiscountPackage> channelDiscountPackageList = channelDiscountPackageService.getListInIds(activityIds, merId);
        for (ChannelDiscountPackage channelDiscountPackage : channelDiscountPackageList) {
            ChannelDiscountPackageResponse response = new ChannelDiscountPackageResponse();
            BeanUtils.copyProperties(channelDiscountPackage, response);
            responses.add(response);
        }
        return responses;
    }
}
